package com.xuan.core.service.pg.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuan.core.dto.pg.PgLiuYaoDto;
import com.xuan.core.mapper.pg.PgLiuYaoMapper;
import com.xuan.core.pojo.pg.PgLiuYao;
import com.xuan.core.service.pg.PgLiuYaoService;
import com.xuan.core.service.sys.SysCountService;
import com.xuan.core.vo.pg.PgLiuYaoVo;
import com.xuan.utils.pg.liuyao.LiuYao;
import com.xuan.utils.pg.liuyao.LiuYaoSetting;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 六爻起卦Impl
 */
@Service
public class PgLiuYaoServiceImpl extends ServiceImpl<PgLiuYaoMapper, PgLiuYao> implements PgLiuYaoService {

    @Autowired
    private SysCountService sysCountService; // 系统统计Service

//********************************************************************************************************************************

    /**
     * 开始起卦
     *
     * @param dto 六爻起卦数据接收类
     * @return 起卦结果
     */
    @Override
    public PgLiuYaoVo starts(PgLiuYaoDto dto) {

        // 1、拷贝六爻起卦设置
        LiuYaoSetting setting = new LiuYaoSetting();
        BeanUtils.copyProperties(dto, setting);

        // 2、六爻起卦
        LiuYao LiuYao = new LiuYao(setting);

        // 3、增加1次总使用数
        sysCountService.saveUsageNumber();

        // 4、封装六爻起卦数据并返回
        return pgLiuYaoVoPackage(dto, LiuYao);

    }

//================================================================================================================================

    /**
     * 六爻起卦数据封装
     *
     * @param dto    六爻起卦数据接收类
     * @param LiuYao 六爻起卦
     * @return 起卦结果
     */
    private PgLiuYaoVo pgLiuYaoVoPackage(PgLiuYaoDto dto, LiuYao LiuYao) {

        PgLiuYaoVo vo = new PgLiuYaoVo();
        BeanUtils.copyProperties(dto, vo); // 设置的数据
        BeanUtils.copyProperties(LiuYao, vo); // 计算的数据
        vo.setSex(dto.getSex() == 0 ? "女" : "男"); // 性别
        return vo;

    }


}
